package org.amphiprion.descentv2assistant.ui.views;

import org.amphiprion.descentv2assistant.R;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ListAdapter;
import android.widget.ListView;

public class PinnedHeaderListView extends ListView {
	private PinnedHeaderAdapter mAdapter;
	private Animation mAnimation;
	private View mHeaderView;
	private int mHeaderViewHeight;
	private boolean mHeaderViewVisible;
	private int mHeaderViewWidth;

	public PinnedHeaderListView(Context paramContext) {
		super(paramContext);
		initAnimations();
	}

	public PinnedHeaderListView(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
		initAnimations();
	}

	public PinnedHeaderListView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
		initAnimations();
	}

	private void initAnimations() {
		this.mAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.show_header);
	}

	public void configureHeaderView(int position) {
		if (mHeaderView == null || mAdapter == null) {
			return;
		}
		switch (this.mAdapter.getPinnedHeaderState(position)) {
		default:
			break;
		case PinnedHeaderAdapter.PINNED_HEADER_GONE:
			this.mHeaderViewVisible = false;
			break;
		case PinnedHeaderAdapter.PINNED_HEADER_VISIBLE:
			this.mAdapter.configurePinnedHeader(this.mHeaderView, position, 255);
			if (this.mHeaderView.getTop() != 0) {
				if (this.mHeaderView.getTop() < 0) {
					this.mHeaderView.startAnimation(this.mAnimation);
				}
				this.mHeaderView.layout(0, 0, this.mHeaderViewWidth, this.mHeaderViewHeight);
			}
			this.mHeaderViewVisible = true;
			break;
		case PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP:
			View localView = getChildAt(0);
			if (localView != null) {
				int i = localView.getBottom();
				// localView.getHeight();
				int j = this.mHeaderView.getHeight();
				int k;
				int m;
				if (i < j) {
					k = i - j;
					m = 255 * (j + k);
				} else {
					k = 0;
					m = 255;
				}
				this.mAdapter.configurePinnedHeader(this.mHeaderView, position, m);
				if (this.mHeaderView.getTop() != k) {
					this.mHeaderView.layout(0, k, this.mHeaderViewWidth, k + this.mHeaderViewHeight);
				}
				this.mHeaderViewVisible = true;
			}
			break;
		}

	}

	@Override
	protected void dispatchDraw(Canvas paramCanvas) {
		super.dispatchDraw(paramCanvas);
		if (this.mHeaderViewVisible) {
			drawChild(paramCanvas, this.mHeaderView, getDrawingTime());
		}
	}

	@Override
	protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
		super.onLayout(paramBoolean, paramInt1, paramInt2, paramInt3, paramInt4);
		if (this.mHeaderView != null) {
			this.mHeaderView.layout(0, 0, this.mHeaderViewWidth, this.mHeaderViewHeight);
		}
		configureHeaderView(getFirstVisiblePosition());

	}

	@Override
	protected void onMeasure(int paramInt1, int paramInt2) {
		super.onMeasure(paramInt1, paramInt2);
		if (this.mHeaderView != null) {
			measureChild(this.mHeaderView, paramInt1, paramInt2);
			this.mHeaderViewWidth = this.mHeaderView.getMeasuredWidth();
			this.mHeaderViewHeight = this.mHeaderView.getMeasuredHeight();
		}
	}

	@Override
	public void setAdapter(ListAdapter paramListAdapter) {
		super.setAdapter(paramListAdapter);
		this.mAdapter = ((PinnedHeaderAdapter) paramListAdapter);
	}

	public void setPinnedHeaderView(View paramView) {
		this.mHeaderView = paramView;
		if (this.mHeaderView != null) {
			setFadingEdgeLength(0);
		}
		requestLayout();
	}

	public static abstract interface PinnedHeaderAdapter {
		public static final int PINNED_HEADER_GONE = 0;
		public static final int PINNED_HEADER_PUSHED_UP = 2;
		public static final int PINNED_HEADER_VISIBLE = 1;

		public abstract void configurePinnedHeader(View paramView, int paramInt1, int paramInt2);

		public abstract int getPinnedHeaderState(int paramInt);
	}
}